<%-- 
    Document   : GAEControl
    Created on : 08-mar-2011, 19:28:17
    Author     : wadjakman
--%>

<%@page import="com.google.appengine.api.datastore.Entity"%>
<%@page import="com.google.appengine.api.datastore.DatastoreServiceFactory"%>
<%@page import="com.google.appengine.api.datastore.DatastoreService"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%! 
  public void dbbrowser_newEntity(HttpServletRequest request)
  {
    String entName = request.getParameter("entName");
    if (entName != null)
    {
      DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 
      Entity entity = new Entity(entName);
      
      String key = null;
      
      for (int f = 0;  ;  f++)
      {
        String pname = request.getParameter("entPropName" + f); 
        if (pname == null  ||  pname.trim().length() == 0) 
          break;
        String value = request.getParameter("entPropValue" + f);
        String isKey = request.getParameter("isKey" + f); 
        if ("on".equals(isKey))
          key = value;
        entity.setProperty(pname, value);
      }
       
      Entity entityToSave = key == null  ?  new Entity(entName)  :  new Entity(entName, key);
      entityToSave.setPropertiesFrom(entity);
      datastore.put(entityToSave);
    } 
  }
%>

<%
  String app = request.getParameter("app");
  String action = request.getParameter("action");
  
  if ("dbbrowser".equals(app))
  {
    if ("newEntity".equals(action))      dbbrowser_newEntity(request);
  }
%>

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>System information & management</title>
    <link rel="stylesheet" type="text/css" href="css/core.css">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script type="text/javascript">
      function onLoad()
      {
        var app = window.location.search.replace(/.*app=([^&]+).*/, "$1");
        var action = window.location.search.replace(/.*action=([^&]+).*/, "$1");
        console.log("app:", app, "action:", action);
        $(".application,.appAction").hide();
        $("#div_" + app).show();
        $("#div_" + action).show();
        $("#" + app).addClass("selectedLink");
        $("#" + action).addClass("selectedLink");
      }
      
      // Entities //////////////////////////////////////////////////////////////
      
      function newEntity_addProperty(target)
      {
        var clone = $("#newEntity_propertyModel").clone().removeClass("closed").addClass("newEntityProperty")[0];
        clone.id = null;
        $("#newEntity_create").before(clone)
      }
      
      function newEntity_removeProperty(target)
      {
        if ($(".newEntityProperty").size() > 1)
          $(target).parent().detach();
      }
      
      function newEntity_send()
      {
        if ($("#entName").val() == "")
        {
          alert("Entity name can't be empty");
          $("#entName").focus();
        }
        else
        {
          $(".newEntityProperty").each(function(index, element) {
            var $children = $(element).children("input");
            $children[0].name = "entPropName" + index;
            $children[1].name = "entPropValue" + index;
            $children[2].name = "isKey" + index;
            if ($children.eq(0).val() == "")
            {
              alert("A property name can't be empty");
              $children[0].focus();
              return false;
            }
          });
          
          document.forms.newEntity.submit();
        }
      }
      
      $(document).ready(onLoad);
    </script>
  </head>
  <body>
    <div class="infoBox">
      <a id="dbbrowser" class="fonts" href="?app=dbbrowser">DB browser</a>
      <hr style="margin: 10px 0px;"/>
      <div id="div_dbbrowser" class="application">
        <a id="newEntity" class="fonts" href="?app=dbbrowser&action=newEntity">New entity</a>
        <a id="listEntities" class="fonts" href="?app=dbbrowser&action=listEntities">List entities</a>
      </div>
      <div id="div_newEntity" class="appAction fonts">
        <hr style="margin: 10px 0px;"/>
        <form name="newEntity" action="GAEControl.jsp?app=dbbrowser&action=newEntity&submit=true" method="POST">
          <input name="app" type="hidden" value="dbbrowser"><input name="action" type="hidden" value="newEntity">
          <div class="vmargin5">
            <label style="width: 200px; display: inline-block;">Entity name</label>
            <input id="entName" name="entName" type="text">
          </div>
          <div id="newEntity_propertyModel" class="vmargin5 closed">
            <label style="width: 200px; display: inline-block;">Property name / value</label>
            <input name="entPropName" type="text">
            <input name="entPropValue" type="text">
            <label class="lmargin10">Is key</label> <input name="isKey" type="checkbox">
            <button type="button" class="hmargin10" onclick="newEntity_addProperty(event.target);">+</button>
            <button type="button" onclick="newEntity_removeProperty(event.target); ">-</button>
          </div>
          <button id="newEntity_create" type="button" onclick="newEntity_send(); ">Create</button>
        </form>
        <script type="text/javascript">
          newEntity_addProperty();
        </script>
      </div>
    </div>
  </body>
</html>
